<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="80px">
      <el-form-item label="名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="商品名称"></el-input>
      </el-form-item>
      <el-form-item label="描述" prop="description">
        <el-input v-model="dataForm.description" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="价格" prop="price">
        <el-input v-model="dataForm.price" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="商家(用户)" prop="userId">
        <el-input v-model="dataForm.userId" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="添加时间" prop="addTime">
        <el-input v-model="dataForm.addTime" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="商品状态" prop="state">
        <el-input v-model="dataForm.state" placeholder="当前商品状态"></el-input>
      </el-form-item>
      <el-form-item label="热度" prop="hot">
        <el-input v-model="dataForm.hot" placeholder="热度"></el-input>
      </el-form-item>
      <el-form-item label="收藏数" prop="follower">
        <el-input v-model="dataForm.follower" placeholder="收藏数"></el-input>
      </el-form-item>
      <el-form-item label="所有图片" prop="picId">
        <el-input v-model="dataForm.picId" placeholder="多个id用;隔开"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          name: '',
          description: '',
          price: '',
          userId: '',
          addTime: '',
          state: '',
          hot: '',
          picId: '',
          follower: ''
        },
        dataRule: {
          name: [
            {required: true, message: '商品名称不能为空', trigger: 'blur'}
          ],
          price: [
            {required: true, message: '不能为空', trigger: 'blur'}
          ],
          userId: [
            {required: true, message: '不能为空', trigger: 'blur'}
          ],
          state: [
            {required: true, message: '当前商品状态不能为空', trigger: 'blur'}
          ],
          hot: [
            {required: true, message: '热度不能为空', trigger: 'blur'}
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/haiyou/goods/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.goods.name
                this.dataForm.description = data.goods.description
                this.dataForm.price = data.goods.price
                this.dataForm.userId = data.goods.userId
                this.dataForm.addTime = data.goods.addTime
                this.dataForm.state = data.goods.state
                this.dataForm.hot = data.goods.hot
                this.dataForm.picId = data.goods.picId
                this.dataForm.follower = data.goods.follower
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/haiyou/goods/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'name': this.dataForm.name,
                'description': this.dataForm.description,
                'price': this.dataForm.price,
                'userId': this.dataForm.userId,
                'addTime': this.dataForm.addTime,
                'state': this.dataForm.state,
                'hot': this.dataForm.hot,
                'picId': this.dataForm.picId,
                'follower': this.dataForm.follower
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
